package org.dromara.business.service.impl;

import jakarta.annotation.Resource;
import org.dromara.business.domain.BusinessInformation;
import org.dromara.business.domain.User;
import org.dromara.business.mapper.BusinessInformationMapper;
import org.dromara.business.service.BusinessInformationService;
import org.dromara.business.service.UserService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.CompanyResumeLibraryBo;
import org.dromara.business.domain.vo.CompanyResumeLibraryVo;
import org.dromara.business.domain.CompanyResumeLibrary;
import org.dromara.business.mapper.CompanyResumeLibraryMapper;
import org.dromara.business.service.CompanyResumeLibraryService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 公司简历库Service业务层处理
 *
 * @author zlj
 * @date 2025-01-22
 */
@RequiredArgsConstructor
@Service
public class CompanyResumeLibraryServiceImpl extends ServiceImpl<CompanyResumeLibraryMapper,CompanyResumeLibrary> implements CompanyResumeLibraryService {

    private final CompanyResumeLibraryMapper baseMapper;
    private final UserService userService;
    @Resource
    @Lazy
    private BusinessInformationService businessInformationService;

    /**
     * 查询公司简历库
     */
    @Override
    public CompanyResumeLibraryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公司简历库列表
     */
    @Override
    public TableDataInfo<CompanyResumeLibraryVo> queryPageList(CompanyResumeLibraryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CompanyResumeLibrary> lqw = buildQueryWrapper(bo);
        Page<CompanyResumeLibraryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<CompanyResumeLibraryVo> records = result.getRecords();
        for (CompanyResumeLibraryVo record : records) {
            BusinessInformation businessInformation = businessInformationService.getById(record.getBusinessInformationId());

            record.setId(record.getBusinessInformationId());
            record.setBt(businessInformation.getBt());
            record.setMoney(businessInformation.getMoney());
            record.setDistricts(businessInformation.getDistricts());
            record.setJobBenefits(businessInformation.getJobBenefits());
            record.setEnterpriseName(businessInformation.getEnterpriseName());
            record.setAge(businessInformation.getAge());
            record.setEducation(businessInformation.getEducation());
            record.setSex(businessInformation.getSex());
        }
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CompanyResumeLibraryVo> getResumes(CompanyResumeLibraryBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        bo.setCompanyId(userId);
        User byId = userService.getById(userId);
        LambdaQueryWrapper<CompanyResumeLibrary> lqw = buildQueryWrapper(bo);
        Page<CompanyResumeLibraryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<CompanyResumeLibraryVo> records = result.getRecords();
        for (CompanyResumeLibraryVo record : records) {
            BusinessInformation businessInformation = businessInformationService.getById(record.getBusinessInformationId());
            record.setAge(businessInformation.getAge());
            record.setEducation(businessInformation.getEducation());
            record.setSex(businessInformation.getSex());
            record.setId(record.getBusinessInformationId());

            record.setBt(businessInformation.getBt());
            record.setMoney(businessInformation.getMoney());
            record.setDistricts(businessInformation.getDistricts());
            record.setJobBenefits(businessInformation.getJobBenefits());
            record.setEnterpriseName(businessInformation.getEnterpriseName());
        }
        return TableDataInfo.build(result);
    }
    /**
     * 查询公司简历库列表
     */
    @Override
    public List<CompanyResumeLibraryVo> queryList(CompanyResumeLibraryBo bo) {
        LambdaQueryWrapper<CompanyResumeLibrary> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CompanyResumeLibrary> buildQueryWrapper(CompanyResumeLibraryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CompanyResumeLibrary> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, CompanyResumeLibrary::getCompanyId, bo.getCompanyId());
        lqw.eq(StrUtil.isNotBlank(bo.getUrl()), CompanyResumeLibrary::getUrl, bo.getUrl());
        lqw.eq(bo.getCreateBy() != null, CompanyResumeLibrary::getCreateBy, bo.getCreateBy());
        lqw.orderByDesc(CompanyResumeLibrary::getCreateTime);
        return lqw;
    }

    /**
     * 新增公司简历库
     */
    @Override
    public Boolean insertByBo(CompanyResumeLibraryBo bo) {
        CompanyResumeLibrary add = MapstructUtils.convert(bo, CompanyResumeLibrary.class);
        Long userId = LoginHelper.getUserId();
        User user = userService.getById(userId);
        if (null != user) {
            if (StringUtils.isNotEmpty(user.getPdf())){
                bo.setCreateBy(userId);
                bo.setCreateTime(new Date());
                bo.setUrl(user.getPdf());
            }else {
                throw new ServiceException("简历为空,请先上传简历");
            }
        }else {
            throw new ServiceException("请登陆!");
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公司简历库
     */
    @Override
    public Boolean updateByBo(CompanyResumeLibraryBo bo) {
        CompanyResumeLibrary update = MapstructUtils.convert(bo, CompanyResumeLibrary.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CompanyResumeLibrary entity){

    }

    /**
     * 批量删除公司简历库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
